package com.jbm.jbmsupplier.presenters;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.jbm.core.iface.IAcross;
import com.jbm.jbmsupplier.activity.rxjavanet.ModeRxjavaNet;
import com.jbm.jbmsupplier.modes.DisupInterface;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.views.DispushViews;
import com.jbm.onehome.iface.IJOrder;
import com.jbm.onehome.iface.IOrder;
import com.jbm.onehome.iface.JobOrderEntity;
import com.jbm.onehome.iface.JordAttachmentEntity;
import com.jbm.onehome.iface.Media;
import com.jbm.onehome.iface.OrdAttachmentEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;

/**
 * Created by 家帮忙 on 2017/7/7.
 */

public class DispushActivitys implements DisupInterface{
    private Context context;
    private DispushViews dispushViews;
    private  DialogLoading dialogLoading;
    private final int WCDD = 0002;
    private final int HUOQUJINE = 0001;

    private final int LOADMEDIA = 0003;
    private final int IORMEDIA = 0004;
    private final int PAYOFFER = 0005;
    private  int orid;
    ArrayList<String> lipic;//报修存放的图片，视频，语音
    ArrayList<HashMap<String, String>> lipics;
    ArrayList<String> lipicsp;
    ArrayList<HashMap<String, String>> lipicss;//施工段存放的图片，视频，语音
    public DispushActivitys(Context context, DispushViews dispushViews) {
        this.context = context;
        this.dispushViews = dispushViews;
    }
    public void setInitMediaS(int sid){//获取订单图片
        initUpMediaS(sid);
    }
    public void getOrderid(int id){//获取订单的id
      this.orid=id;
  }
    @Override
    public void initUpMediaS(int sid) {
        dialogLoading = new DialogLoading(context, "加载中...", false);
        dialogLoading.show();
        NetIntent netintetn = new NetIntent(handler);
        netintetn.setLoadPic(sid, IORMEDIA);
    }

    @Override
    public void initUpMedia(int id) {
        dialogLoading = new DialogLoading(context, "加载中...", false);
        dialogLoading.show();
        NetIntent netintetn = new NetIntent(handler);
        netintetn.setLoadPic(id, LOADMEDIA);
    }
    public void getShowJine(int id){
        setShowJine(id);
    }

//    public void getQuedingOrder(int id){//确认订单
//        setQuedingOrder(id);
//    }

//    public void getXianxiaOrder(int id){//线下付款
//        setXianxiaOrder(id);
//    }
    @Override
    public void setQuedingOrder(int id) {//确认订单
        NetIntent netIntent = new NetIntent(handler);
        netIntent.setConfirm(id, WCDD);
        dialogLoading = new DialogLoading(context, "确认中...", true);
        dialogLoading.show();
    }

    @Override
    public void setXianxiaOrder(int id) {//线下付款
        NetIntent netintent = new NetIntent(handler);
        netintent.payOffLine(id, PAYOFFER);
        dialogLoading = new DialogLoading(context, "确认中...", true);
        dialogLoading.show();
    }
    @Override
    public void setShowJine(int id) {
        NetIntent netIntent = new NetIntent(handler);
        netIntent.setAmount(id, HUOQUJINE);
    }
    @Override
    public void setInitDate() {
        lipic = new ArrayList<>();
        lipics = new ArrayList<>();
        lipicsp = new ArrayList<>();
        lipicss = new ArrayList<>();
    }

    @Override
    public void showDingDanPic(List<OrdAttachmentEntity> orderdindanpic) {//获取报修管理端订单图片
        for (int i = 0; i <orderdindanpic.size() ; i++) {
            HashMap<String, String> has = new HashMap<>();
            String attpath = orderdindanpic.get(i).getAttPath();
            String in = "http://media.jiabangmang.com/" + attpath.substring(attpath.indexOf("a") + 1, attpath.length());
            String ins = "http://192.168.0.16:8080/media/" + attpath.substring(attpath.indexOf("a") + 1, attpath.length());
//            if (orderdindanpic.get(i).getAttType().equals("pic")){
//            lipic.add(ins);}
            has.put("memo",  orderdindanpic.get(i).getMemo());
            has.put("pic", in);
            has.put("type", orderdindanpic.get(i).getAttType());
            lipics.add(has);
        }
        dispushViews.setOrderDatas(lipic,lipics);
    }

    @Override
    public void showGongDanPic(List<JordAttachmentEntity> jorderdindanpic) {//获取报修管理端工单图片
        for (int i = 0; i <jorderdindanpic.size() ; i++) {
            HashMap<String, String> has = new HashMap<>();
            String attpath = jorderdindanpic.get(i).getAttPath();
            String in = "http://media.jiabangmang.com/" + attpath.substring(attpath.indexOf("a") + 1, attpath.length());
            String ins = "http://192.168.0.16:8080/media/" + attpath.substring(attpath.indexOf("a") + 1, attpath.length());
//            if (jorderdindanpic.get(i).getAttType().equals("pic")){
//            lipicsp.add(ins);}

            has.put("pic", in);
            has.put("memo",  jorderdindanpic.get(i).getMemo());
            has.put("type", jorderdindanpic.get(i).getAttType());
            lipicss.add(has);

        }
        dispushViews.setjOrderData(lipicsp,lipicss);
    }

    /**
     * 上传工单图片
     * 的状态
     */
    @Override
    public void ShangChuanGDTupian(final int orid, String sta, File file) {
        dialogLoading = new DialogLoading(context, "上传中...", false);
        dialogLoading.show();
        final Media media1 = new Media();
        media1.mtype = "pic";
        media1.filepath = file.getPath();
        media1.filename =file.getName();
        media1.m_memo=sta;
        ModeRxjavaNet.Companion.getInstantMode().objects(Observable.create(new Observable.OnSubscribe<Media>() {
            @Override
            public void call(Subscriber<? super Media> subscriber) {

                subscriber.onNext(ModeRxjavaNet.Companion.getInstantMode().getOhInter().UploadMedia(orid,"job_order",media1));
                ;
            }
        })).subscribe(new Observer<Media>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Media media) {
                if (media.m_code==200){
                    dialogLoading.dismiss();
                }else{
                    dispushViews.setEerror(media.m_info,dialogLoading);
                }

            }
        });







    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HUOQUJINE: {
                    IOrder orderd = (IOrder) msg.obj;
                    if (orderd.m_code == 200) {
                        if (orderd.amount != null) {
                            dispushViews.setWeiXiuAmout(orderd.amount);
                            initUpMedia(orid); //获取完成订单的照片
                        }
                    }
                    break;
                }
                case WCDD: {
                    IJOrder jor = (IJOrder) msg.obj;
                    if (jor.m_code == 200) {

                        dispushViews.setSucced(dialogLoading,1);
                    } else if (jor.equals("网络超时")) {
                        dispushViews.setEerror(jor.m_info,dialogLoading);
                    } else {
                        dispushViews.setEerror(jor.m_info,dialogLoading);
                    }

                    break;
                }
                case LOADMEDIA: {
                    JobOrderEntity dow = (JobOrderEntity) msg.obj;
                    if (dow.m_code == 200) {
                        if (dow == null)
                            return;
                        if (dow != null) {
                            if (dow.getJordAttachmentList() != null){
                            for (int i = 0; i < dow.getJordAttachmentList().size(); i++) {
                                HashMap<String, String> has = new HashMap<>();
                                JordAttachmentEntity attpath = dow.getJordAttachmentList().get(i);
                                String pathurl = attpath.getAttPath();
                                String in = "http://media.jiabangmang.com/" + pathurl.substring(pathurl.indexOf("a") + 1, pathurl.length());
                                lipic.add(in);
                                has.put("pic", in);
                                lipics.add(has);
                            }}
                            if (dow.getIorderEntity() != null) {
                                if (dow.getIorderEntity().getOrdattachmentlist() != null) {
                                    for (int i = 0; i < dow.getIorderEntity().getOrdattachmentlist().size(); i++) {
                                        HashMap<String, String> has = new HashMap<>();
                                        OrdAttachmentEntity attpat = dow.getIorderEntity().getOrdattachmentlist().get(i);
                                        String pathur = attpat.getAttPath();
                                        String ins = "http://media.jiabangmang.com/" + pathur.substring(pathur.indexOf("a") + 1, pathur.length());
                                        has.put("pic", ins);
                                        lipics.add(has);
                                        lipic.add(ins);
                                    }
                                }
                            }
                            dispushViews.setOrderData(lipic,lipics);
                            dispushViews.setSucced(dialogLoading,0);
                        }
                    } else if (dow.m_info.equals("网络超时")) {
                        dispushViews.setEerror(dow.m_info,dialogLoading);
                    } else {
                        dispushViews.setEerror(dow.m_info,dialogLoading);
                    }
                    break;
                }
                case IORMEDIA: {
                    JobOrderEntity dows = (JobOrderEntity) msg.obj;
                    if (dows.m_code == 200) {
                        if (dows == null)
                            return;
                        if (dows != null) {
                            if (dows.getIorderEntity() == null) {
                                dispushViews.setSucced(dialogLoading,0);
                                return;
                            }
                            if (dows.getIorderEntity().getOrdattachmentlist() == null) {
                                dispushViews.setSucced(dialogLoading,0);
                                return;
                            }
                            for (int i = 0; i < dows.getIorderEntity().getOrdattachmentlist().size(); i++) {
                                HashMap<String, String> has = new HashMap<>();
                                OrdAttachmentEntity attpath = dows.getIorderEntity().getOrdattachmentlist().get(i);
                                String pathurl = attpath.getAttPath();
                                String in = "http://media.jiabangmang.com/" + pathurl.substring(pathurl.indexOf("a") + 1, pathurl.length());
                                String ins = "http://192.168.0.16:8080/media/" + pathurl.substring(pathurl.indexOf("a") + 1, pathurl.length());
                                has.put("pic", ins);
                                lipics.add(has);
                                lipic.add(ins);
                            }
                            dispushViews.setOrderData(lipic,lipics);
                            dispushViews.setSucced(dialogLoading,0);
                        }
                    } else if (dows.equals("网络超时")) {
                        dispushViews.setEerror(dows.m_info,dialogLoading);
                    } else {
                        dispushViews.setEerror("",dialogLoading);
                    }
                    break;
                }
                case PAYOFFER: {
                    IAcross ic = (IAcross) msg.obj;
                    if (ic != null) {
                        if (ic.m_code == 200) {
                            Toast.makeText(context, "成功支付", Toast.LENGTH_SHORT).show();

                            dispushViews.setSucced(dialogLoading,3);
                        } else if (ic.m_code == 500) {
                            dispushViews.setEerror(ic.m_info,dialogLoading);
                        } else {

                            dispushViews.setEerror(ic.m_info,dialogLoading);
                        }
                    }
                    break;
                }
            }
        }
    };


}
